/*************************************************************************
    > File Name: 2opendir.c
    > Author: Ykk
    > Mail: 3393579535@qq.com 
    > Created Time: 2023年04月08日 星期六 10时54分12秒
 ************************************************************************/

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/types.h>
#include<unistd.h>
#include<dirent.h>
#include<sys/stat.h>

//目录打开和关闭
int main()
{
	DIR *dir = NULL;
	struct dirent *d = NULL;

	//1.打开目录
	dir = opendir("../test");
	if(NULL == dir)
	{
		perror("opendir");
		return 1;
	}


	//2.循环读取目录内容
	while(1)
	{
		//读取目录的内容
		d = readdir(dir);
		if(NULL == d)
		{
			perror("readdir");
			return 1;
		}
	//	printf("d_type:%hu d_name:%s\n",d->d_type,d->d_name);
		if(d->d_type == DT_REG)
		{
		printf("d_type:普通文件  d_name:%s\n",d->d_name);
		}
		printf("d_type:%hu d_name:%s\n",d->d_type,d->d_name);
	}
	closedir(dir);

	return 0;
}
